//
//  MainFirst_MagazineList_UgcCell_WX.m
//  ZITHER
//
//  Created by weixin on 2018/10/15.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainFirst_MagazineList_UgcCell_WX.h"

@implementation MainFirst_MagazineList_UgcCell_WX

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configComment];
    [self setUI];
    [self setMasonry];
}

-(void)setModel:(CoverCreateListModelSub *)model
{
    _model = model;
    
    self.userHead.model = model;
    
    if ([self.recommend isEqualToString:@"1"]) {
        self.userHead.hidden = NO;
        [self.userHead mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
        }];
        self.userPic.hidden = YES;
        self.userLab.hidden = YES;
        [self.viewBorder mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(90);
        }];
    } else {
        self.userHead.hidden = YES;
        [self.userHead mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        self.userPic.hidden = NO;
        self.userLab.hidden = NO;
        [self.viewBorder mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
        }];
    }
    
    
    [self.userPic sd_setImage:model.avatar];
    self.userLab.text = model.nickName;
    self.title.text = model.ugcContent;

    if (model.nickName.length>0) {
//        [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.userPic.mas_bottom).offset(9);
//        }];
        
        [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userPic.mas_bottom).offset(9);

        }];
    }else{
//        [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.userPic.mas_bottom).offset(0);
//        }];
        [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userPic.mas_bottom).offset(0);
        }];
    }
    
    
    if ( model.ugcFileList.count == 0) {

        BaseImageV *imageV = self.arrPic[0];
        [imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenW-60);
            make.height.mas_equalTo(0);
        }];
        imageV.hidden = YES;
        imageV = self.arrPic[1];
        imageV.hidden = YES;
        imageV = self.arrPic[2];
        imageV.hidden = YES;
        
    }else if (model.ugcFileList.count == 1){
        
        CGFloat widthPic = (kScreenW-60)/315*157;
        BaseImageV *imageV = self.arrPic[0];
        imageV.hidden = NO;
        [imageV sd_setImage:model.ugcFileList[0]];
        [imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenW-60);
            make.height.mas_equalTo(widthPic);
        }];
        imageV = self.arrPic[1];
        imageV.hidden = YES;
        imageV = self.arrPic[2];
        imageV.hidden = YES;

    }else if (model.ugcFileList.count == 2){
        CGFloat width = (kScreenW - 60-20)/3.0;

        BaseImageV *imageV = self.arrPic[0];
        imageV.hidden = NO;
        [imageV sd_setImage:model.ugcFileList[0]];
        [imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(width);
        }];
        imageV = self.arrPic[1];
        [imageV sd_setImage:model.ugcFileList[1]];
        imageV.hidden = NO;
        imageV = self.arrPic[2];
        imageV.hidden = YES;
        
    }else{
        CGFloat width = (kScreenW - 60-20)/3.0;

        BaseImageV *imageV = self.arrPic[0];
        imageV.hidden = NO;
        [imageV sd_setImage:model.ugcFileList[0]];
        [imageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(width);
        }];
        imageV = self.arrPic[1];
        [imageV sd_setImage:model.ugcFileList[1]];
        imageV.hidden = NO;
        imageV = self.arrPic[2];
        [imageV sd_setImage:model.ugcFileList[2]];
        imageV.hidden = NO;
        
    }

    
    if (model.ugcContent.length != 0 && model.ugcFileList.count != 0) {
//        self.title.text = model.ugcContent;
//        [self.ImageScrV addScrImage:model.ugcFileList];
//        [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self).offset(22);
//        }];
//        [self.ImageScrV mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.title.mas_bottom).offset(12);
//            make.height.mas_equalTo(60);
//        }];
    }else{
        if (model.ugcFileList.count == 0) {
//            self.ImageScrV.scrollEnabled = YES;
//            self.title.text = model.ugcContent;
//            [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self).offset(22);
//            }];
//            [self.ImageScrV mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.title.mas_bottom).offset(0);
//                make.height.mas_equalTo(0);
//            }];
//        }else{
//            [self.ImageScrV addScrImage:model.ugcFileList];
//            [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self).offset(0);
//            }];
//            [self.ImageScrV mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.title.mas_bottom).offset(12);
//                make.height.mas_equalTo(60);
//            }];
        }
    }

    
    if ([model.relType isEqualToString:@"1"]){
        if (![model.commentCount isEqualToString:@"0"]) {
            self.commentCountLab.hidden = NO;
            self.commentCountLab.text = [NSString stringWithFormat:@"%@条评论",model.commentCount];
        }else{
            self.commentCountLab.hidden = YES;
        }
        
    }else{
        if (![model.correlationUserCount isEqualToString:@"0"]) {
            self.commentCountLab.hidden = NO;
            self.commentCountLab.text = [NSString stringWithFormat:@"%@人参与",model.correlationUserCount];
        }else{
            self.commentCountLab.hidden = YES;
        }
        
    }
    if (self.State == 1) {
        self.commentCountLab.hidden = YES;
    }
    

  
   
    if (model.userSubjectTitle.length>0 ) {
        self.TopicLab.text = [NSString stringWithFormat:@"    %@",model.userSubjectTitle];
        self.TopicIcon.hidden = NO;
        [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userPic.mas_bottom).offset(9);

        }];
    }else{
        self.TopicIcon.hidden = YES;
        self.TopicLab.text = @"";
        [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userPic.mas_bottom).offset(0);

            
        }];
    }
    

}

/** 用户进入自己个人主页自己头像 */
-(void)setHiddenUserInfo
{
    self.userPic.hidden = YES;
    self.userLab.hidden = YES;

    [self.userPic mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.width.height.mas_equalTo(0);
    }];
    
}

-(void)setMasonry{
    [self.userHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(60);
    }];
    
    
    [self.userPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userHead.mas_bottom).offset(15);
        make.left.equalTo(self).offset(30);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.userLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userPic.mas_centerY).offset(0);
        make.left.equalTo(self.userPic.mas_right).offset(9);
        make.height.mas_equalTo(13);
        make.width.mas_equalTo(150);

    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_bottom).offset(9);
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
    }];
    
    [self.commentCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userPic.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-30);
        make.height.mas_equalTo(13);
    }];
    
    
    [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPic.mas_bottom).offset(9);
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
//        make.height.mas_equalTo(30);
    }];
    
    [self.TopicIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_top).offset(5);
        make.left.equalTo(self.TopicLab.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(10, 13));
    }];
    
    [self setPicMasonry];
    
}



/** 设置三图的约束 */
-(void)setPicMasonry{
    
    CGFloat width = (kScreenW - 60-20)/3.0;
    
    for (int i = 0; i<3; i++) {
        BaseImageV *imageV = self.arrPic[i];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.title.mas_bottom).offset(5);
            make.left.equalTo(self).offset(30+(width + 10)*i);
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(width);
            if (i==0) {
                make.bottom.equalTo(self).offset(-50);
            }
        }];
    }

}

-(void)setUI{
    [self addSubview:self.title];
    [self addSubview:self.userPic];
    [self addSubview:self.userLab];
    [self addSubview:self.userHead];
    [self addSubview:self.commentCountLab];
    [self addSubview:self.TopicLab];
    [self addSubview:self.TopicIcon];


}

-(void)setRecommend:(NSString *)recommend{
    _recommend = recommend;
}

-(void)configComment{
    [self setBorderWithHiddem:YES Color:baseColorLight width:1 Radius:8];
}

- (BaseLab *)title{
    if (!_title) {
        _title = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:15 textColor:[UIColor colorWithHex:@"333333"] isBold:NO numberOfLines:4];
        _title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];

    }
    return _title;
}

-(BaseImageV *)userPic{
    if (!_userPic) {
        _userPic = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        [_userPic setCornerRadiusWithRadius:15 isMasks:YES];
        
    }
    return _userPic;
}

-(BaseLab *)userLab{
    if (!_userLab) {
        _userLab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:12 textColor:[UIColor colorWithHex:@"333333"] isBold:YES numberOfLines:0];
    }
    return _userLab;
}

-(NSMutableArray *)arrPic{
    if (!_arrPic) {
        _arrPic = [[NSMutableArray alloc]init];
        for (int i = 0 ; i<3; i++) {
            BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
            imageV.tag = 200+i;
            [self addSubview:imageV];
            [_arrPic addObject:imageV];
        }
    }
    return _arrPic;
}

-(MainFirst_MagazineList_UserHeadCell_WX *)userHead{
    if (!_userHead) {
        _userHead = [[MainFirst_MagazineList_UserHeadCell_WX alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 65)];
    }
    return _userHead;
}

-(BaseLab *)commentCountLab{
    if (!_commentCountLab) {
        _commentCountLab = [BaseLab initCustomLab:@"条评论" color:HexColor(0x999999) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:12]];
    }
    return _commentCountLab;
}

-(BaseLab *)TopicLab{
    if (!_TopicLab) {
        _TopicLab = [BaseLab initCustomLab:@"    1232131" color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFangSC-Semibold" size:15]];
        _TopicLab.numberOfLines = 2;
        _TopicLab.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _TopicLab;
}

-(BaseImageV *)TopicIcon{
    if (!_TopicIcon) {
        _TopicIcon = [[BaseImageV alloc]init];
        
        _TopicIcon.image = [UIImage imageNamed:@"发布_#话题"];
        _TopicIcon.backgroundColor = [UIColor clearColor];
    }
    return _TopicIcon;
}

@end
